package com.xlf.common.controller;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.xlf.cache.OrderStatusList;
import com.xlf.common.model.Order;
import com.xlf.common.model.OrderCatalog;

public class OrderController extends Controller {

	public void index() {
		String oid = getPara("oid");
		String createTime = getPara("createTime");
		String designerName = getPara("designerName");
		int status = Integer.parseInt(getPara("status", "-1"));

		setAttr("oid", oid);
		setAttr("createTime", createTime);
		setAttr("designerName", designerName);
		setAttr("status", Integer.toString(status));

		Page<Order> pageRecord = Order.dao.exeSearchSql(getParaToInt("page", 1), 20, oid, null, createTime, designerName, status);
		setAttr("pageRecord", pageRecord);

		setAttr("statusList", OrderStatusList.getList());
		renderJsp("index.jsp");
	}

	public void detail() {
		String oid = getPara("oid");
		Order order = Order.dao.findById(oid);
		List<OrderCatalog> orderCatalogs = order.getOrderCatalogs();
		
		setAttr("order", order);
		setAttr("orderCatalogs", orderCatalogs);

		String print = getPara("print");
		if(print != null) {
			// 订单打印页面
			renderJsp("print.jsp");
		} else {
			renderJsp("form.jsp");
		}
	}
	
	/**
	 * 审批通过
	 */
	public void orderApply() {
		String oid = getPara("oid");
		int status = getParaToInt("status");
		Order order = Order.dao.findById(oid);
		order.set("status", status);
		order.update();
		
		// by zyk 订单创建成功审批并开启后，发送邮件
//		if(MailProperties.getIsstart().equals("on")){
//			SendMail sendMail = new SendMail();
//			sendMail.send(order);
//		}
		
		detail();
	}
	
	/**
	 * 删除订单
	 */
	public void orderDel() {
		String oid = getPara("doid");
		Order order = Order.dao.findById(oid);
		if(order != null && order.getInt("status") == 0) {
			Order.dao.deleteOrderInfo(oid);
		}
		
		index();
	}

}
